//
//  DishControl.m
//  XRmenu
//
//  Created by gaomz on 11-3-29.
//  Copyright 2011 E0571. All rights reserved.
//

#import "DishControl.h"



@implementation DishControl
@synthesize product;
@synthesize productIndex;
@synthesize productCount;
@synthesize deleagate;
@synthesize audioPlayer;

-(id)initWithViewName:(NSString *)vname 
{
	NSDictionary* aDic = [self findNode:APPSESSION.allconfig withKey:vname];
	if ((self=[super initWithConfig:aDic])) {
		
		//addNObserver(@selector(audioPlayerStoped:),kMessage_AudioPlayer_Stop);
		
		
		
		btnAudio = (UIButton *)[self viewWithTag:kTagOfStartBT];
		btnVideo=(UIButton *)[self viewWithTag:kTagOfStartBT+1];
		btnRelate=(UIButton *)[self viewWithTag:kTagOfStartBT+2];
		lblRelateNum=(UILabel *)[self viewWithTag:kTagOfStartLabel];
		imgCommend=(UIImageView *)[self viewWithTag:kTagOfStartImgV];
		lblDishName=(UILabel *)[self viewWithTag:kTagOfStartLabel+1];
		lbldishPrice=(UILabel *)[self viewWithTag:kTagOfStartLabel+2];
		imgLine=(UIImageView *)[self viewWithTag:kTagOfStartImgV+1];
		imgLinePoint=(UIImageView *)[self viewWithTag:kTagOfStartImgV+2];
		btnDishNum=(UIButton *)[self viewWithTag:kTagOfStartBT+6];
		btnDetail = (UIButton *)[self viewWithTag:kTagOfStartBT+4];
		
		normalImage=[[btnAudio imageForState:UIControlStateNormal] retain];
		selectedImage=[[btnAudio imageForState:UIControlStateSelected] retain];
		//	hightlightedImage=[btnAudio imageForState:UIControlStateHighlighted];
		//	disabledImage=[btnAudio imageForState:UIControlStateDisabled];
		
		
		
		
		
		isPlaySound=NO;
		
		
		audioFrame = btnAudio.frame;
		videoFrame = btnVideo.frame ;
		relateFrame= btnRelate.frame ;
		relateNumFrame = lblRelateNum.frame ;
		commendFrame= imgCommend.frame ;
		dishNameFrame=lblDishName.frame;
		dishPriceFrame=lbldishPrice.frame;
		lineFrame=imgLine.frame ;
		linePointFrame=imgLinePoint.frame ;
		
		NSError *error=nil;
		NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:[XRmenuPublic getGeneralSkinImg:@"View/A_ButtonClicked.mp3"]];
		audioPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error]; 		
		[audioPlayer prepareToPlay];
		audioPlayer.volume = 0.0; // set hitPlayer's volume
		audioPlayer.numberOfLoops = 0 ;
		[audioPlayer play];
		[fileURL release];
    }
    return self;
}


-(void)refreshControlStatus:(Product *)cProduct cProductIndex:(NSInteger)cProductIndex cProductCount:(NSInteger)cProductCount
{
	btnAudio.hidden = NO;
	btnVideo.hidden = NO;
	btnRelate.hidden = NO;
	lblRelateNum.hidden = NO;
	imgCommend.hidden = NO;
	lblDishName.hidden = NO;
	lbldishPrice.hidden=NO;
	imgLine.hidden=NO;
	imgLinePoint.hidden=NO;
	
	btnAudio.frame = audioFrame ;
	btnVideo.frame = videoFrame;
	btnRelate.frame = relateFrame;
	lblRelateNum.frame = relateNumFrame;
	imgCommend.frame = commendFrame;
	lblDishName.frame = dishNameFrame;
	lbldishPrice.frame=dishPriceFrame;
	imgLine.frame=lineFrame;
	imgLinePoint.frame=linePointFrame;
	
	
	product=cProduct;
	productIndex=cProductIndex;
	productCount=cProductCount;
	
	NSString *audioFileName;
	NSString *videoFileName;
	XRmenuError *error=nil;
	audioFileName=[XRmenuPublic haveAudioFile:product Error:&error];
	if (error) {
		btnAudio.hidden=YES;
	}
	error=nil;
	videoFileName=[XRmenuPublic haveVedioFile:product Error:&error];
	if (error) {
		btnVideo.hidden=YES;
	}
	
	if ([product.pIsRecommend intValue]==0) {
		imgCommend.hidden=YES;
	}
	lbldishPrice.text=[NSString stringWithFormat:@"%@%d/%@",[APPSESSION.allconfig getStringValueAtPath:@"System Config/currency"],[product.pSalePrice intValue],[XRmenuPublic jsonfield:product.pUnit]];
	
	lblDishName.text=[XRmenuPublic jsonfield:product.pName];
	
	NSArray *arrRelate = [XRmenuPublic haveRelationProduct:product];
	if ([arrRelate count]>0) {
		lblRelateNum.text = [NSString stringWithFormat:@"(%d)",[arrRelate count]];
	}
	else {
		btnRelate.hidden=YES;
		lblRelateNum.hidden=YES;
	}

	
	NSInteger dishNum = [XRmenuPublic getProductNum:product];
	[btnDishNum setTitle:[NSString stringWithFormat:@"%d",dishNum] forState:UIControlStateNormal];
	[btnDishNum setTitle:[NSString stringWithFormat:@"%d",dishNum] forState:UIControlStateSelected];
	[btnDishNum setTitle:[NSString stringWithFormat:@"%d",dishNum] forState:UIControlStateHighlighted];
	if (dishNum>0) {
		btnDishNum.hidden=NO;
	}
	else {
		btnDishNum.hidden=YES;
	}

	if ([product.pType intValue]==ProductTypeDish) {
		btnDetail.hidden=NO;
	}
	else{
		btnDetail.hidden=YES;
	}
	
	[UIView beginAnimations: @"animation" context: nil];
	[UIView setAnimationDuration: 0.0];
	[UIView setAnimationDelegate:self];
	
	
	if (btnAudio.hidden) {
		btnVideo.frame=CGRectMake(btnAudio.frame.origin.x, btnVideo.frame.origin.y, btnVideo.frame.size.width, btnVideo.frame.size.height);
		btnRelate.frame=CGRectMake(relateFrame.origin.x - btnVideo.frame.origin.x + btnAudio.frame.origin.x, btnRelate.frame.origin.y, btnRelate.frame.size.width, btnRelate.frame.size.height);
		lblRelateNum.frame=CGRectMake(relateNumFrame.origin.x - btnVideo.frame.origin.x + btnAudio.frame.origin.x, lblRelateNum.frame.origin.y, lblRelateNum.frame.size.width, lblRelateNum.frame.size.height);
	//	imgCommend.frame=CGRectMake(commendFrame.origin.x- btnVideo.frame.origin.x + btnAudio.frame.origin.x, imgCommend.frame.origin.y, imgCommend.frame.size.width, imgCommend.frame.size.height);
//		lblDishName.frame=CGRectMake(dishNameFrame.origin.x- btnVideo.frame.origin.x + btnAudio.frame.origin.x, dishNameFrame.origin.y, lblDishName.frame.size.width, lblDishName.frame.size.height);
//		lbldishPrice.frame=CGRectMake(dishPriceFrame.origin.x- btnVideo.frame.origin.x + btnAudio.frame.origin.x, dishPriceFrame.origin.y, lbldishPrice.frame.size.width, lbldishPrice.frame.size.height);
	}
	if (btnVideo.hidden) {
	//	lbldishPrice.frame=CGRectMake(lbldishPrice.frame.origin.x -btnRelate.frame.origin.x + btnVideo.frame.origin.x, dishPriceFrame.origin.y, lbldishPrice.frame.size.width, lbldishPrice.frame.size.height);
	//	lblDishName.frame=CGRectMake(lblDishName.frame.origin.x-btnRelate.frame.origin.x + btnVideo.frame.origin.x, dishNameFrame.origin.y, lblDishName.frame.size.width, lblDishName.frame.size.height);
	//	imgCommend.frame=CGRectMake(imgCommend.frame.origin.x-btnRelate.frame.origin.x + btnVideo.frame.origin.x, imgCommend.frame.origin.y, imgCommend.frame.size.width, imgCommend.frame.size.height);
		lblRelateNum.frame=CGRectMake(lblRelateNum.frame.origin.x - btnRelate.frame.origin.x + btnVideo.frame.origin.x , lblRelateNum.frame.origin.y, lblRelateNum.frame.size.width, lblRelateNum.frame.size.height);
		btnRelate.frame=CGRectMake(btnVideo.frame.origin.x, btnRelate.frame.origin.y, btnRelate.frame.size.width, btnRelate.frame.size.height);
	}
	//if (btnRelate.hidden) {
//	//	lbldishPrice.frame=CGRectMake(lbldishPrice.frame.origin.x-imgCommend.frame.origin.x + btnRelate.frame.origin.x, dishPriceFrame.origin.y, lbldishPrice.frame.size.width, lbldishPrice.frame.size.height);
////		lblDishName.frame=CGRectMake(lblDishName.frame.origin.x-imgCommend.frame.origin.x + btnRelate.frame.origin.x, dishNameFrame.origin.y, lblDishName.frame.size.width, lblDishName.frame.size.height);
////		imgCommend.frame=CGRectMake(btnRelate.frame.origin.x, imgCommend.frame.origin.y, imgCommend.frame.size.width, imgCommend.frame.size.height);
//	}
//	if (imgCommend.hidden) {
//		lbldishPrice.frame=CGRectMake(lbldishPrice.frame.origin.x-lblDishName.frame.origin.x + imgCommend.frame.origin.x, dishPriceFrame.origin.y, lbldishPrice.frame.size.width, lbldishPrice.frame.size.height);
//		lblDishName.frame=CGRectMake(imgCommend.frame.origin.x, dishNameFrame.origin.y, lblDishName.frame.size.width, lblDishName.frame.size.height);
//	}
	
	
	imgLine.frame=CGRectMake(lblDishName.frame.origin.x,lineFrame.origin.y , lineFrame.size.width, lineFrame.size.height);
	CGFloat linePointWidth = [num(productIndex) floatValue]/[num(productCount) floatValue]*lineFrame.size.width;
	imgLinePoint.frame=CGRectMake(imgLine.frame.origin.x+linePointWidth, linePointFrame.origin.y, linePointFrame.size.width, linePointFrame.size.height);
	
	[UIView commitAnimations];
	
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
}
*/




#pragma mark 按钮点击事件
-(void) buttonClicked:(id)sender
{
	NSInteger tag=[sender tag];
	switch (tag) {
		case kTagOfStartBT:	
			[self PlaySound];
			break;
		case kTagOfStartBT+1:
			[deleagate DisplayVideo];
			break;
		case kTagOfStartBT+2:
			[APPSESSION setRelateDishLayerStatus:(1- [APPSESSION relateDishLayerStatus])];
			[deleagate refreshRelateStatus];
			break;
		case kTagOfStartBT+3:
			[deleagate switchToAlbum];
			break;
		case kTagOfStartBT+4:
			[deleagate DisplayDetail];
			break;
		case kTagOfStartBT+5:
			[deleagate AddDish];
			break;
		default:
			[deleagate AddDish];
			break;
	}
	DebugLog(@"请重载本方法处理按钮事件%i", [sender tag]);
}


#pragma mark 声音播放结束

-(void)PlaySound
{	
	DebugLog(@"======================================");
	if (isPlaySound) {
		[self StopSound];
	}
	else {
		NSString *audioFileName;
		XRmenuError *error=nil;
		audioFileName=[XRmenuPublic haveAudioFile:product Error:&error];
		if (error) {
			btnAudio.hidden=YES;
		}
		else {
			//[APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"View/Photo View/music.m4r"]];
			[self playSoundOnEvent:audioFileName];	
			isPlaySound=YES;
		}
	}		
	
	[self changeAudioState];
}
-(void)StopSound
{

	[audioPlayer stop];
	isPlaySound=FALSE;
	[self changeAudioState];
}

// 播放音乐
-(void) playSoundOnEvent:(NSString *)mp3File{
	NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:mp3File];
	[audioPlayer initWithContentsOfURL:fileURL error:nil];
	[audioPlayer stop];
	[audioPlayer prepareToPlay];
	audioPlayer.volume = 0.7; // set hitPlayer's volume
	audioPlayer.numberOfLoops = 0 ;
	[audioPlayer setDelegate:self];
	[audioPlayer play];
	[fileURL release];
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)playedPlayer successfully:(BOOL)flag {
	isPlaySound=FALSE;
	[self changeAudioState];
}

//-(void)audioPlayerStoped:(NSNotification*)notification
//{
//
//		isPlaySound=FALSE;
//		[self changeAudioState];
//
//}


-(void)changeAudioState
{
	if (!isPlaySound) {
		[btnAudio setImage:normalImage forState:UIControlStateNormal];
		[btnAudio setImage:selectedImage forState:UIControlStateSelected];
		//	[btnAudio setImage:hightlightedImage forState:UIControlStateDisabled];
		//	[btnAudio setImage:disabledImage forState:UIControlStateHighlighted];
		
	}
	else {
		[btnAudio setImage:selectedImage forState:UIControlStateNormal];
		[btnAudio setImage:normalImage forState:UIControlStateSelected];
	}
	
	
}



#pragma mark 语言切换
-(void) changeLanguage:(NSNotification *)notification
{
	// todo: 切换语言
	NSDictionary *dict = [APPSESSION.allconfig findAtPath:[NSString stringWithFormat:@"UI Skin/%@/Dish Main View/Dish Photo View/Dish Control View", APPSESSION.uistyle]];
	if (dict) {
		NSArray *bts = (NSArray*)[dict objectForKey:@"button list"];
		[self translateButtons:bts];
		
		//更好推荐图片
		UIImage* img = [UIImage imageWithContentsOfFile: locimg(@"View/Photo View/commend.png")]; 
		UIImageView* imgV =(UIImageView*)[self viewWithTag:kTagOfStartImgV];
		[imgV setImage:img]; 
		
		
		
		[self refreshControlStatus:product cProductIndex:productIndex cProductCount:productCount];
	}
	
}


- (void)dealloc {
	//removeNObserver(kMessage_AudioPlayer_Stop);
	//[product release];
	[normalImage release];
	[selectedImage release];
	[audioPlayer release];
    [super dealloc];
}


@end
